package com.example.museum.controller;



import com.example.museum.common.Result;
import com.example.museum.entity.ProductAppraise;
import com.example.museum.entity.User;
import com.example.museum.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户
 */

@RestController
@RequestMapping("user")
@Slf4j
public class UserContriller {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @param session
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestBody User user,HttpSession session){
        log.info("当前登陆的用户:{}",user);
        User userLogin = userService.login(user);
        if (userLogin == null) {
            return Result.failed("登录失败");
        }
        return Result.success(userLogin);
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("register")
    public Result register(@RequestBody User user){
        userService.register(user);
        return Result.success("注册成功");
    }


    /**
     * 退出
     * @param request
     * @return
     */
    @GetMapping("out")
    public Result logout(HttpServletRequest request) {
        log.info("退出");
//        BaseContext.clear();
        return Result.success("退出成功");
    }


    /**
     *   获取用户评论
     * @param productAppraise
     * @return
     */
    @GetMapping("appraiselist")
    public Result<List<ProductAppraise>> appraiselist(@RequestBody ProductAppraise productAppraise) {
        List<ProductAppraise> list= userService.getappraise(productAppraise);
      return Result.success(list);
    }


}
